/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');
var qr = require('qr-image');
var memberController = {
    getmemberPage: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            var include = [{ model: seq.wxUsersModule }];
            paramT.include = include;
            //根据id
            if (req.query.id != undefined) {
                params["id"] = req.query.id;
            }
            if (req.query.openId != undefined) {
                params["openId"] = req.query.openId;
            }
            //模糊查询
            if (req.query.name != undefined) {
                params["name"] = { "$like": "%" + req.query.name + "%" };
            }
            if (req.query.tel != undefined) {
                params["tel"] = { "$like": "%" + req.query.tel + "%" };
            }
            if (req.query.pTitle != undefined) {
                params["$or"] = { "name": { "$like": "%" + req.query.pTitle + "%" }, "tel": { "$like": "%" + req.query.pTitle + "%" } };
            }
            if (req.query.type != undefined) {
                params["type"] = parseInt(req.query.type);
            }
            if (req.query.province != undefined) {
                params["province"] = { "$like": "%" + req.query.province + "%" };

            } if (req.query.city != undefined) {
                params["city"] = { "$like": "%" + req.query.city + "%" };
            }
            if (req.query.cTitle != undefined) {
                params["$or"] = { "province": { "$like": "%" + req.query.cTitle + "%" }, "city": { "$like": "%" + req.query.cTitle + "%" } };
            }
            if (req.query.hospitalName != undefined) {
                params["hospitalName"] = { "$like": "%" + req.query.hospitalName + "%" };
            }
            if (req.query.hospitalOffices != undefined) {
                params["hospitalOffices"] = { "$like": "%" + req.query.hospitalOffices + "%" };
            }
            if (req.query.wxTitle != undefined) {
                params["$or"] = { "name": { "$like": "%" + decodeURI(req.query.wxTitle) + "%" }, "hospitalName": { "$like": "%" + decodeURI(req.query.wxTitle) + "%" }, "hospitalOffices": { "$like": "%" + decodeURI(req.query.wxTitle) + "%" } };
            }
            if (req.query.filed1 != undefined) {
                params["filed1"] = { "$like": "%" + req.query.filed1 + "%" };
            }
            if (req.query.filed2 != undefined) {
                params["filed2"] = { "$like": "%" + req.query.filed2 + "%" };
            }
            if (req.query.filed3 != undefined) {
                params["filed3"] = { "$like": "%" + req.query.filed3 + "%" };
            }
            if (req.query.filed4 != undefined) {
                params["filed4"] = { "$like": "%" + req.query.filed4 + "%" };
            }
            if (req.query.filed5 != undefined) {
                params["filed5"] = { "$like": "%" + req.query.filed5 + "%" };
            }
            if (req.query.filed8 != undefined) {
                params["filed8"] = req.query.filed8;
            }
            paramT["where"] = params;
            paramT["order"] = [["createTime", 'DESC']];
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            seq.memberModule.findAndCountAll(paramT).then(function (result) {
                res.json({ "success": true, "page": page, "total": result.count, "result": result.rows });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    qrImage: function (req, res) {
        var text = req.query.text;
        try {
            var img = qr.image(text, { size: 10 });
            res.writeHead(200, { 'Content-Type': 'image/png' });
            img.pipe(res);
        } catch (e) {
            res.writeHead(414, { 'Content-Type': 'text/html' });
            res.end('<h1>414 Request-URI Too Large</h1>');
        }
    }

}
module.exports = memberController;